﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Attributes
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class EnumRequiredAttribute : RequiredAttribute
    {
        const string NOT_SET_VALUE = "None";
        public string NotSetValue { get; set; }

        public EnumRequiredAttribute()
            : this(NOT_SET_VALUE)
        { }

        public EnumRequiredAttribute(string notSetValue)
            : base()
        {
            if (String.IsNullOrEmpty(notSetValue))
                throw new ArgumentNullException("notSetValue");

            NotSetValue = notSetValue;
        }

        public override bool IsValid(object value)
        {
            if (value == null)
                return false;

            var notSet = Enum.Parse(value.GetType(), NotSetValue);

            return !Enum.Equals(value, notSet);
        }
    }
}
